﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xejen.Database.SqlSugar
{
    /// <inheritdoc cref="IUnitOfWork"/>
    public class UnitOfWork<TDbContext> : IUnitOfWork
        where TDbContext : SqlSugarClient
    {
        private readonly TDbContext _dbContext;

        /// <inheritdoc cref="UnitOfWork{TDbContext}"/>
        public UnitOfWork(IDbContextProvider<TDbContext> provider)
        {
            _dbContext = provider.GetDbContext();
        }

        /// <inheritdoc/>
        public void BeginTransaction()
        {
            _dbContext.BeginTran();
        }

        /// <inheritdoc/>
        public async Task BeginTransactionAsync()
        {
            await _dbContext.BeginTranAsync();
        }

        /// <inheritdoc/>
        public void Commit()
        {
            _dbContext.RollbackTran();
        }

        /// <inheritdoc/>
        public async Task CommitAsync()
        {
            await _dbContext.RollbackTranAsync();
        }

        /// <inheritdoc/>
        public void Rollback()
        {
            _dbContext.RollbackTran();
        }

        /// <inheritdoc/>
        public async Task RollbackAsync()
        {
            await _dbContext.RollbackTranAsync();
        }

        /// <inheritdoc/>
        public void Dispose()
        {
            //_dbContext.Dispose();
        }
    }
}
